package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.YddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Create by Boller on 2022/2/27 21:18
 */

@SuppressWarnings("All")
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {

    @Autowired
    private YddUserService yddUserService;

    /**
     * 用户管理——分页查询
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String sex, String username, Long mobile, Integer page, Integer limit) {
        Page<YddUser> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(username), YddUser::getUsername, username);
        queryWrapper.eq(StringUtils.isNotEmpty(sex), YddUser::getSex, sex);
        queryWrapper.like(mobile != null, YddUser::getMobile, mobile);

        yddUserService.page(pageInfo, queryWrapper);

        return ResultData.ok(PageResult.init(pageInfo));

    }

    /**
     * 用户管理--根据id查询单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        return ResultData.ok(yddUserService.getById(id));
    }

    /**
     * 用户管理--根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {

        yddUserService.removeById(id);
        return ResultData.ok("删除成功");
    }

    /**
     * 用户管理--修改
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddUser yddUser) {

        yddUserService.updateById(yddUser);
        return ResultData.ok("修改成功");
    }

    /**
     * 用户管理--添加
     *
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddUser yddUser) {
        yddUserService.save(yddUser);
        return ResultData.ok("添加成功");
    }

}
